package controller;

import model.HashPromulgatorToReceiver;
import model.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: a
 * Date: 2022-12-08
 * Time: 16:41
 */
@WebServlet("/connect")
public class Connect extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf8");
        //1. 获取到用户的登录状态
        HttpSession session = req.getSession(false);
        if(session == null){
            resp.setStatus(403);
            return;
        }
        User curUser = (User) session.getAttribute("user");
        if(curUser == null){
            resp.setStatus(403);
            return;
        }
        int curId = curUser.getUserid();
        User user = Search.user;
        if(user == null){
            return;
        }
        HashPromulgatorToReceiver hashPromulgatorToReceiver = new HashPromulgatorToReceiver();
        if(!hashPromulgatorToReceiver.find(curId, user)) {
            hashPromulgatorToReceiver.insert(curId, user);
        }
        System.out.println("111");
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf8");
        //1. 获取到用户的登录状态
        HttpSession session = req.getSession(false);
        if(session == null){
            resp.setStatus(403);
            return;
        }
        User curUser = (User) session.getAttribute("user");
        if(curUser == null){
            resp.setStatus(403);
            return;
        }
        int curId = curUser.getUserid();
        User user = Search.user;
        if(user == null){
            return;
        }
        HashPromulgatorToReceiver hashPromulgatorToReceiver = new HashPromulgatorToReceiver();
        if(hashPromulgatorToReceiver.find(curId, user)) {
            hashPromulgatorToReceiver.delete(curId, user);
        }
        System.out.println("222");
    }
}
